<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台首页</title>
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <script src="../js/jquery/1.x/jquery.js?v=1"></script>
    <script src="../layui/layui.js?v=1" charset="utf-8"></script>
    <script src="../js/function.js?v=1" type="application/javascript"></script>
    <link rel="stylesheet" href="../layui/css/layui.css?v=1" media="all">
    <link rel="stylesheet" href="../css/layout.css?v=1" media="all">
    <script src="../js/config.js?v=1"></script>
    <link rel="stylesheet" href="../css/public.css?v=1" media="all">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo">
            <img src="../img/logo2.png">
        </div>
        <!-- 头部区域（可配合layui已有的水平导航） -->
        <ul class="layui-layout-right">
            <li class="layui-nav-item header-userinfo">
                <div class="top">
                    <p class="username">韩三区 欢迎登录</p>
                    <p class="exit-btn">
                        <img src="../img/tuichu.png">
                        退出
                    </p>
                </div>
                <div class="bottom">
                    <p>
                        <span class="user_center_name">系统升级测试医院</span>
                        <span>|</span>
                        <span class="dept_name">内分泌内科</span>
                    </p>
                </div>
            </li>
            <li class="layui-nav-item touxiang">
                <img src="../img/touxiang2.png">
            </li>
            <div class="clear"></div>
        </ul>
    </div>

    <div class="layui-side layui-border-box">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->

            <ul class="layui-nav layui-nav-tree">
                <li class="layui-nav-item shouye">
                    <a href="./indexboard.html" data-href="" data-icon="shouye">
                        <img class="nav-icon" src="../img/shouye.png">首页
                    </a>
                </li>
                <li class="layui-nav-item bingli layui-this">
                    <a href="javascript:;" data-href="./patient/list.html" data-icon="bingliguanli" data-title="病历管理" data-nav_id="2">
                        <img class="nav-icon" src="../img/bingliguanli_baise.png">病历管理
                    </a>
                </li>
                <li class="layui-nav-item suifang">
                    <a href="javascript:;" data-href="./follow/list.html" data-icon="suifangguanli" data-title="随访管理" data-nav_id="3">
                        <img src="../img/suifangguanli_lanse.png">随访管理
                    </a>
                </li>
                <li class="layui-nav-item tongji">
                    <a href="javascript:;" data-href="./tongji.html" data-icon="tongjifenxi" data-title="统计分析" data-nav_id="4">
                        <img class="nav-icon" src="../img/tongjifenxi_lanse.png">统计分析
                    </a>
                </li>
            </ul>
        </div>
    </div>

    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div class="layui-header">
            <div class="layui-logo" id="iframe-title">病历管理</div>
            <div class="hospital">
                <p>当前中心：<span class="center_name">系统医院</span></p>
                <button class="change-center" type="button">切换中心</button>
            </div>
        </div>
        <div class="iframe-mask" id="iframe-mask"></div>
        <div class="iframe-container">
            <iframe class="admin-iframe" name="admin-iframe" id="admin-iframe" src="./patient/list.html"></iframe>
        </div>
    </div>
</div>
<div class="fixed-container change-center-container">
    <div class="fixed-main">
        <div class="fixed-title">
            <p>切换中心</p>
            <i class="layui-icon layui-icon-close fixed-main-close"></i>
        </div>
        <div class="fixed-form">
            <form class="layui-form">
                <div class="fixed-form-item">
                    <button type="button" id="userCenter">本中心</button>
                </div>
                <div class="fixed-form-item layui-form-item">
                    <select name="center_id" id="change_center_select" class="layui-select" lay-filter="change_center_select">
                    </select>
                </div>
                <div class="fixed-form-item">
                    <button style="background:#4777E7;color:white;" type="button" class="change_center_confirm" lay-submit="" lay-filter="change_center_confirm">确定</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    //JavaScript代码区域
    $(document).ready(function () {
        //iframe高度设置，屏幕高度减header，减layui-body-header，减padding20
        window.onload=function(){
            changeIframeHeight();
        }
        window.onresize=function(){
            changeIframeHeight();
        }
        function changeIframeHeight(){
            var height = $(window).height() - 60 - 51 - 20;
            console.log(height)
            $('.iframe-container').height(height);
        };

        var nav_id = getLocalStorage('nav_id');
        var curr_href = getLocalStorage('curr_href');

        ajax_get(configUrl.getMenuList,'',(res)=>{
            console.log(res)
            var arr = [];
            $.each(res.data,function (index,item) {
                arr.push(item.id);
            })
            if (!arr.includes(1)) {
                $('.hospital button').remove();
            }
            if (!arr.includes(2)) {
                $('.layui-nav .bingli').remove();
            }
            if (!arr.includes(3)) {
                $('.layui-nav .suifang').remove();
            }
            if (!arr.includes(4)) {
                $('.layui-nav .tongji').remove();
            }
        })
        if(curr_href != null || curr_href !=''){
            $('iframe').attr('src',curr_href);
        }
        if(nav_id == 1){
            $('.layui-nav .layui-nav-item:nth-child(2)').addClass('layui-this');
            $('.layui-nav .layui-nav-item:nth-child(2)').siblings().removeClass('layui-this');
            var href = './patient/add.html';
            var icon = $('.layui-nav .layui-nav-item:nth-child(2) a').attr('data-icon');
            var iframeTitle = $('.layui-nav .layui-nav-item:nth-child(2) a').attr('data-title')
            var iconSrcLanse = '../img/' + icon + '_lanse.png';
            var iconSrcBaise = '../img/' + icon + '_baise.png';
            $('.layui-nav .layui-nav-item:nth-child(2) a').children('img').attr('src', iconSrcBaise);
            $('#iframe-title').text(iframeTitle);
            //$('iframe').attr('src', href);
        }else if(nav_id == 2){
            $('.layui-nav .layui-nav-item:nth-child(2)').addClass('layui-this');
            $('.layui-nav .layui-nav-item:nth-child(2)').siblings().removeClass('layui-this');
            var href = $('.layui-nav .layui-nav-item:nth-child(2) a').attr('data-href');
            var icon = $('.layui-nav .layui-nav-item:nth-child(2) a').attr('data-icon');
            var iframeTitle = $('.layui-nav .layui-nav-item:nth-child(2) a').attr('data-title')
            var iconSrcLanse = '../img/' + icon + '_lanse.png';
            var iconSrcBaise = '../img/' + icon + '_baise.png';
            $('.layui-nav .layui-nav-item:nth-child(2) a').children('img').attr('src', iconSrcBaise);
            $('#iframe-title').text(iframeTitle);
            //$('iframe').attr('src', href);
        }else if(nav_id == 3){
            $('.layui-nav .layui-nav-item:nth-child(3)').addClass('layui-this');
            $('.layui-nav .layui-nav-item:nth-child(3)').siblings().removeClass('layui-this');
            var href = $('.layui-nav .layui-nav-item:nth-child(3) a').attr('data-href');
            var icon = $('.layui-nav .layui-nav-item:nth-child(3) a').attr('data-icon');
            var iframeTitle = $('.layui-nav .layui-nav-item:nth-child(3) a').attr('data-title')
            var iconSrcLanse = '../img/' + icon + '_lanse.png';
            var iconSrcBaise = '../img/' + icon + '_baise.png';
            $('.layui-nav .layui-nav-item:nth-child(3) a').children('img').attr('src', iconSrcBaise);
            $('.layui-nav .layui-nav-item:nth-child(2) a').children('img').attr('src', '../img/'+$('.layui-nav .layui-nav-item:nth-child(2) a').attr('data-icon')+'_lanse.png');
            $('#iframe-title').text(iframeTitle);
            //$('iframe').attr('src', href);
        }else if(nav_id == 4){
            $('.layui-nav .layui-nav-item:nth-child(4)').addClass('layui-this');
            $('.layui-nav .layui-nav-item:nth-child(4)').siblings().removeClass('layui-this');
            var href = $('.layui-nav .layui-nav-item:nth-child(4) a').attr('data-href');
            var icon = $('.layui-nav .layui-nav-item:nth-child(4) a').attr('data-icon');
            var iframeTitle = $('.layui-nav .layui-nav-item:nth-child(4) a').attr('data-title')
            var iconSrcLanse = '../img/' + icon + '_lanse.png';
            var iconSrcBaise = '../img/' + icon + '_baise.png';
            $('.layui-nav .layui-nav-item:nth-child(4) a').children('img').attr('src', iconSrcBaise);
            $('.layui-nav .layui-nav-item:nth-child(2) a').children('img').attr('src', '../img/'+$('.layui-nav .layui-nav-item:nth-child(2) a').attr('data-icon')+'_lanse.png');
            $('#iframe-title').text(iframeTitle);
            //$('iframe').attr('src', href);
        }
        layui.use(['element','form','layer'], function () {
            var element = layui.element;
            var layer = layui.layer;
            var jq = layui.jquery;
            var form = layui.form;
            $('.layui-nav-item a').click(function () {
                var href = $(this).attr('data-href');
                var icon = $(this).attr('data-icon');
                var nav_id = $(this).attr('data-nav_id');
                saveLocalStorage('nav_id',nav_id)
                saveLocalStorage('curr_href',href)
                var iframeTitle = $(this).attr('data-title');

                var iconSrcBaise = '../img/' + icon + '_baise.png';

                //其他icon变为蓝色
                $(this).parent().siblings().each(function(){
                    var icon = $(this).children('a').attr('data-icon')
                    var iconSrcLanse = '../img/' + icon + '_lanse.png';
                    $(this).children('a').children('img').attr('src', iconSrcLanse);
                })
                //首页不变
                $('.shouye a img').attr('src','../img/shouye.png')
                //被点击的变成白色
                $(this).children('img').attr('src', iconSrcBaise);
                $('#iframe-title').text(iframeTitle);
                $('iframe').attr('src', href);
            });

            //左边菜单点击
            jq('.layui-side-scroll').on('click', '.layui-nav-item', function () {
                jq("#iframe-mask").show();
                //遮罩层消失
                jq("#admin-iframe").load(function () {
                    jq("#iframe-mask").fadeOut(100);
                });
            });

            //面包屑导航链接点击
            $('#iframe-title').on('click','a[class="mianbaoxie"]',function(){
                console.log('1111');
                var href = $(this).attr('data-href');
                var iframeTitle = $(this).attr('data-title');
                $('#iframe-title').text(iframeTitle);
                $('iframe').attr('src', href);
            })

            //退出登录
            $('.exit-btn').click(function(){
                window.localStorage.clear();
                layer.msg('退出成功',{icon:1,time:1000},function(){
                    window.location.href='./login.html';
                });
            })
            //用户信息存储
            var userInfo = JSON.parse(getLocalStorage('userInfo'));
            console.log(userInfo)
            if (!userInfo && typeof (userInfo) != "undefined" && userInfo != 0) {
                layer.msg('请重新登录', {icon: 5, time: 1500}, function () {
                    // window.location.href = './login.html';
                });
            }
            $('.username').text(userInfo.user_name + ' 欢迎登录');
            $('.user_center_name').text(userInfo.center_name);
            $('.center_name').text(getLocalStorage('center_name'));
            $('.dept_name').text(userInfo.dept_name);
            $('.touxiang img').attr('src', userInfo.photo);

            var centerList = '';
            //切换中心
            $('.change-center').click(function () {
                $('.fixed-container').show();
            });
            $('.fixed-main-close').click(function () {
                $('.fixed-container').hide();
            });
            //本中心
            $('#userCenter').click(function(){
                var nav_id = getLocalStorage('nav_id');
                console.log(nav_id);
                saveLocalStorage('hosp_id',userInfo.hosp_id);
                saveLocalStorage('dept_id',userInfo.dept_id);
                saveLocalStorage('center_name',userInfo.center_name);
                layer.msg('切换成功', {icon: 1, time: 1000},function(){
                    if(nav_id == 2){
                        saveLocalStorage('curr_href','/template/patient/list.html');
                    }
                    window.location.reload();
                    $('.fixed-container').hide();
                });
            })
            //切换确认
            $('.change_center_confirm').click(function(){
               var key = $('#change_center_select').val();
                var nav_id = getLocalStorage('nav_id');
                console.log(nav_id);
               if(centerList == ''){
                   layer.msg('数据为空', {icon: 5, time: 1500});
               }else{
                   var hosp_id = centerList[key].hosp_id;
                   var dept_id = centerList[key].dept_id;
                   saveLocalStorage('hosp_id',hosp_id);
                   saveLocalStorage('dept_id',dept_id);
                   saveLocalStorage('center_name',centerList[key].center_name);
                   $('.center_name').text(centerList[key].center_name);
                   //$('.dept_name').text(centerList[key].dept_name);
                   layer.msg('切换成功', {icon: 1, time: 1000},function(){
                       if(nav_id == 2){
                           saveLocalStorage('curr_href','/template/patient/list.html');
                       }
                       window.location.reload();
                       $('.fixed-container').hide();
                   });
               }
            })

            getHospital(form);
            function getHospital(form){
                console.log('getHospital');
                ajax_get(configUrl.getHospital,'',(res)=>{
                    console.log(res);
                    if(res.status == 0){
                        centerList = res.data;
                        var str0 = '<option value="">选择其他中心<i class="layui-icon layui-icon-triangle-d"></i></option>'
                        $('#change_center_select').append(str0);
                        $.each(res.data,function(index,item){
                            var selected = '';
                            if(getLocalStorage('dept_id') == item.dept_id){
                                selected == 'selected';
                            }
                            var str = '<option value="'+index+'" '+selected+'>'+item.center_name+'</option>'
                            $('#change_center_select').append(str);
                        })
                        form.render();
                    }else{
                        layer.msg(res.msg, {icon: 5, time: 1500});
                    }
                })
            }
        });
    })
</script>
</body>
</html>
